import json
import uuid

import requests
import json as complexjson
from common.logger import logger


class RpcClient():

    def __init__(self, api_root_url):
        self.api_root_url = api_root_url
        self.id = str(uuid.uuid4())
        self.data = {
            "id": self.id,
            "params": {},
            "jsonrpc": "2.0"
        }
        # self.session = requests.session()

    def post(self, method, params={}, **kwargs):
        return self.request(method, params, **kwargs)

    def request(self, method, params={}, **kwargs):
        url = self.api_root_url
        self.data["method"] = method
        self.data["params"] = params
        self.request_log(url)
        resp = requests.post(url, json=self.data, **kwargs)
        logger.info("接口响应结果 ==>> {}".format(json.loads(resp.content.decode('utf-8'))))
        return resp
        # if method == "POST":

    def request_log(self, url, **kwargs):
        logger.info("接口请求地址 ==>> {}".format(url))
        logger.info("接口请求参数 ==>> {}".format(self.data))
